package cn.hairui.blog.controller.manage;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.model.Tool;
import cn.hairui.blog.model.User;
import cn.hairui.blog.service.ToolService;
import cn.hairui.blog.util.DateUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * <p>
 * <h2>请添加说明
 * </h2>
 * <ol>请添加详细说明
 * </ol>
 *
 * <h2>修改历史
 * </h2>
 * <ol>如有修改，请添加修改历史
 * </ol>
 *
 * </p>
 *
 * @author laoganbu
 * @version V1.0
 * @date 2019/8/9 16:36
 */
@Controller
@RequestMapping(value = "/manage")
public class ToolManageController {

    @Autowired
    private ToolService toolService;

    //列表页面
    @RequestMapping(value = "page/tool/toolList")
    public String toolList() {
        return "manage/page/tool/toolList";
    }

    //新增页面
    @RequestMapping(value = "page/tool/toolAdd")
    public String toolAdd() {
        return "manage/page/tool/toolAdd";
    }

    //修改页面
    @RequestMapping(value = "page/tool/toolUpdate")
    public String toolUpdate(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        Tool tool = toolService.find(id);
        model.addAttribute("tool", tool);
        return "manage/page/tool/toolUpdate";
    }

    //列表数据
    @RequestMapping(value = "page/tool/list")
    @ResponseBody
    public String list(HttpServletRequest request) {
        Map map = new HashMap<Object, Object>();
        int pageSize = 5;//默认每页显示5条记录
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("limit"));
        } catch (Exception e) {
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Map> toolList = toolService.listMgr();

        PageInfo<Map> pageInfo = new PageInfo<Map>(toolList);
        map.put("data", toolList);
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return JSONUtils.toJSONString(map);
    }


    //新增数据
    @RequestMapping(value = "page/tool/add")
    @ResponseBody
    public String add(HttpServletRequest request, Tool tool) {
        Map map = new HashMap<Object, Object>();

        tool.setAddDate(DateUtil.getCurrentDate());
        int serno = toolService.add(tool);
        Logger.getLogger(this.getClass().getName()).info("新增工具编号：" + serno);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //更新数据
    @RequestMapping(value = "page/tool/update")
    @ResponseBody
    public String update(HttpServletRequest request, Tool tool) {
        Map map = new HashMap<Object, Object>();
        toolService.update(tool);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }

    //删除数据
    @RequestMapping(value = "page/tool/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@Param(value = "id") int id, HttpServletRequest request) {
        Map map = new HashMap();
        //判断权限
        User userinfo = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (userinfo == null || !PubConstant.YES_NO_Y.equals(userinfo.getAdminflag())) {
            //普通用户或者游客 无权限删除文章
            map.put(PubConstant.flag, PubConstant.permissions);
        } else {
            //管理员
            toolService.delete(id);
            map.put(PubConstant.flag, PubConstant.success);
        }
        return JSONUtils.toJSONString(map);
    }
}
